package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<SetmealDto> setmealDtoList = setmealMapper.findByPage(name);
        PageInfo<SetmealDto> pageInfo = new PageInfo<>(setmealDtoList);
        Page<SetmealDto> pageList = new Page<>();
        pageList.setRecords(pageInfo.getList());
        pageList.setTotal(pageInfo.getTotal());
        return pageList;
    }

    @Override
    @Transactional
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    public void save(SetmealDto setmealDto) {
        LocalDateTime now = LocalDateTime.now();
        setmealDto.setCreateTime(now);
        setmealDto.setUpdateTime(now);
        setmealDto.setStatus(1);
        setmealMapper.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        if (setmealDishes.size() != 0) {
            setmealDishes.forEach(item -> {
                item.setSetmealId(setmealDto.getId());
                item.setCreateTime(setmealDto.getCreateTime());
                item.setUpdateTime(setmealDto.getUpdateTime());
                item.setCreateUser(setmealDto.getCreateUser());
                item.setUpdateUser(setmealDto.getUpdateUser());
                item.setSort(0);
            });
            setmealDishMapper.save(setmealDishes);
        }
    }

    @Override
    public SetmealDto findById(long id) {
        SetmealDto setmealDto = setmealMapper.findById(id);
        return setmealDto;
    }

    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void update(SetmealDto setmealDto) {
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealMapper.update(setmealDto);
        setmealDishMapper.deleteById(setmealDto.getId());

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.forEach(item->{
            item.setUpdateUser(setmealDto.getUpdateUser());
            item.setUpdateTime(LocalDateTime.now());
        if(item.getSetmealId()==null){
            item.setSetmealId(setmealDto.getId());
            item.setCreateUser(setmealDto.getCreateUser());
            item.setCreateTime(LocalDateTime.now());
            item.setSort(0);
            item.setUpdateUser(setmealDto.getUpdateUser());
            item.setUpdateTime(LocalDateTime.now());
        }
        });
        setmealDishMapper.save(setmealDishes);
    }

    /**
     * 根据类别id与状态查询套餐列表
     *
     * @param categoryId 类别的id
     * @param status     启用状态
     * @return List 套餐列表
     */
    @Override
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status",unless = "#result==null")
    public List<Setmeal> findAllByCidAndStatus(Long categoryId, Integer status) {
        return setmealMapper.findAllByCidAndStatus(categoryId,status);
    }

    @Override
    public void updateStatus(List<Long> ids, Integer status) {
        setmealMapper.updateStatus(ids,status);
    }

    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void delete(List<Long> ids) {
        setmealMapper.delete(ids);
    }
}
